package com.wly.login.user.strategy;

import cn.hutool.core.util.StrUtil;
import com.wly.login.common.ValidationException;
import com.wly.login.user.entity.User;
import com.wly.login.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author wly
 */
@Service
public class PhoneLoginStrategy implements LoginStrategy{

	@Override
	public String loginType() {
		return "password";
	}
	@Resource
	private UserService userService;

	@Override
	public void login(User user) {
		String phone = user.getPhone();
		String verificationCode = user.getVerificationCode();
		// 1、参数校验
		if (StrUtil.isBlank(phone)) {
			throw new ValidationException("电话不能为空");
		}
		if (StrUtil.isBlank(verificationCode)) {
			throw new ValidationException("验证码不能为空");
		}
		// 2、验证用户是否存在
		User userInfo = userService.findByPhone(phone);
		if (userInfo == null) {
			throw new ValidationException("电话不存在");
		}
		// 3、TODO 短信验证码校验
		user.setId(userInfo.getId());
		// 实现账号密码登录的逻辑
		System.out.println("电话登录 - 电话: " + phone + ", 验证码: " + verificationCode);
	}
}
